package org.lq.ht.view.room;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.Room;
import org.lq.ht.entity.TypeID;
import org.lq.ht.service.impl.RoomServiceImpl;
import org.lq.ht.util.CastUtils;
import org.lq.ht.util.ShowMessage;
import org.lq.ht.util.WindowsSplit;
/**房间视图
 * @author 黄明慧
 *
 * @package org.lq.ht.view.room
 * @date 2020年8月21日下午11:29:16
 */
public class TwoRoomTable2 extends JPanel {

	private JPanel contentPane;
	private JTextField textField;
	private static JTable table_left;
	private static JTable table_right;
	private RoomServiceImpl roomService = new RoomServiceImpl();
	private JSplitPane splitPane;
	private JPopupMenu popupMenu;
	private List<Room> roomList = new ArrayList<>();
	private List<Room> rightTableList = new ArrayList<>();
	private double sumRoomMoney=0;
	private AddCustomer ac;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TwoRoomTable2 frame = new TwoRoomTable2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TwoRoomTable2() {
		setBounds(100, 100, 826, 514);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		
		splitPane = new JSplitPane();
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel.add(panel_1, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("空净房间");
		panel_1.add(lblNewLabel);
		
		textField = new JTextField();
		panel_1.add(textField);
		textField.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);
		popupMenu = new JPopupMenu();
		
		
		JMenuItem mntmNewMenuItem = new JMenuItem("选房");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int rowIndex = table_left.getSelectedRow();
				if(rowIndex>=0) {
					Object obj = table_left.getValueAt(rowIndex, 0);
					int id = CastUtils.castInt(obj);
					Room rom = roomService.getById(id);
					//如果选中表格时没有找到rom对象,也即是选中了表头表尾或者不在表格上选择
					if(rom!=null) {
						//迭代器删除list集合中的对象
						 Iterator<Room> it =roomList.iterator();
					        while (it.hasNext())
					        {
					        	Room rom2 = it.next();
					           if(rom2.getRoom_number()==rom.getRoom_number()) {
					        	   rom2.setStatus("已选");
					        	   ShowMessage.panelMessage(TwoRoomTable2.this, true,"房间号为: "+rom.getRoom_number()+"选房成功");
					        	   sumRoomMoney+=CastUtils.castDouble(rom.getRoom_price());
					        	   rightTableList.add(rom2);
					        	   it.remove();
					           }
					        }
					        initTable(roomList,table_left,true);
					        initTable(rightTableList, table_right,false);
					}
					
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		table_left = new JTable();
		table_left.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"id", "\u5BA2\u623F\u53F7", "\u623F\u95F4\u7C7B\u578B", "\u5168\u65E5\u623F\u4EF7", "\u623F\u95F4\u8BF4\u660E"
			}
		));
		table_left.getColumnModel().getColumn(0).setPreferredWidth(1);
		table_left.getColumnModel().getColumn(0).setMinWidth(1);
		table_left.getColumnModel().getColumn(0).setMaxWidth(1);
		scrollPane.setViewportView(table_left);
		
		JPanel panel_2 = new JPanel();
		splitPane.setRightComponent(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_3, BorderLayout.NORTH);
		
		JLabel lblNewLabel_1 = new JLabel("已选客房");
		panel_3.add(lblNewLabel_1);
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("按房间类型自动联动");
		panel_3.add(chckbxNewCheckBox);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_2.add(scrollPane_1, BorderLayout.CENTER);
		
		table_right = new JTable();
		table_right.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"id", "\u5BA2\u623F\u53F7", "\u623F\u95F4\u7C7B\u578B", "\u5168\u65E5\u623F\u4EF7", "\u623F\u95F4\u8BF4\u660E"
			}
		));
		table_right.getColumnModel().getColumn(0).setPreferredWidth(1);
		table_right.getColumnModel().getColumn(0).setMinWidth(1);
		table_right.getColumnModel().getColumn(0).setMaxWidth(1);
		scrollPane_1.setViewportView(table_right);
		
		JPopupMenu popupMenu_1 = new JPopupMenu();
		panel_2.add(popupMenu_1, BorderLayout.SOUTH);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("取消");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int rowIndex = table_right.getSelectedRow();
				if(rowIndex>=0) {
					Object obj = table_right.getValueAt(rowIndex, 0);
					int id = CastUtils.castInt(obj);
					Room rom = roomService.getById(id);
					////如果选中表格时没有找到rom对象,也即是选中了表头表尾或者不在表格上选择
					if(rom!=null) {
						//迭代器删除list集合中的对象
						 Iterator<Room> it =rightTableList.iterator();
					        while (it.hasNext())
					        {
					        	Room rom2 = it.next();
					           if(rom2.getRoom_number()==rom.getRoom_number()) {
					        	   sumRoomMoney-=CastUtils.castDouble(rom.getRoom_price());
					        	   ShowMessage.panelMessage(TwoRoomTable2.this, true, "房间号为: "+rom.getRoom_number()+"已取消");
					        	   rom2.setStatus("空房");
					        	   roomList.add(rom2);
					        	   it.remove();
					           }
					        }
					        initTable(roomList,table_left,true);
					        initTable(rightTableList, table_right,false);
						
					}
					
				}
			}
		});
		popupMenu_1.add(mntmNewMenuItem_1);
		
		JPanel panel_4 = new JPanel();
		contentPane.add(panel_4, BorderLayout.SOUTH);
		
		JButton btnNewButton_4 = new JButton("添加客人");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		panel_4.add(btnNewButton_4);
		
		JButton btnNewButton = new JButton("会员房间");
		panel_4.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("保存订房信息");
		btnNewButton_1.addActionListener(new ActionListener() {
			//循环添加修改房间状态,如果房间状态没有改变则不进行修改操作
			public void actionPerformed(ActionEvent e) {
				if(rightTableList.size()==0) {
					ShowMessage.panelMessage(TwoRoomTable2.this, false, "你还没有选择任何房间哦");
				}else if(roomService.saveRoomList(rightTableList)){
					ShowMessage.panelMessage(TwoRoomTable2.this, true, "恭喜你,已经全部成功选房!");
				}else {
					//原因是因为如果在销售员选房的时候,可能某些客人已经通过自主选房把房间选走
					ShowMessage.panelMessage(TwoRoomTable2.this, false, "你所选房间可能部分错误");
				}
				
			}
		});
		panel_4.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("New button");
		panel_4.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("关闭");
		panel_4.add(btnNewButton_3);
		splitPane.addComponentListener(new WindowsSplit() {
			@Override
			public void componentResized(ComponentEvent e) {
				splitPane.setDividerLocation(0.5);
			}
		});
		addPopup(table_left, popupMenu,table_left);
		addPopup(table_right,popupMenu_1,table_right);
		initTable(roomList,table_left,true);
		initTable(rightTableList, table_right,false);
	}
	/**
	 * 根据客户选个的房间类型展示符合条件的房间信息
	 * @param list
	 * @param table
	 * @param bool
	 */
	public void initTable(List<Room> list,JTable table,boolean bool) {
			int counts = table.getRowCount();
			DefaultTableModel dtm = (DefaultTableModel) table.getModel();
			for (int i = 0; i < counts; i++) {
				dtm.removeRow(0);
			}
				list.forEach((e)->{
					dtm.addRow(new Object[] {
							e.getNo(),
							e.getRoom_number(),
							e.getType(),
							e.getRoom_price(),
							e.getNote()
					});
				});
			
			if(list.size()>0) {
				if(bool) {
					dtm.addRow(new Object[] {
							null,
							"可用房间:",
							list.size()+"间",
							null,
							null
					});
				}else {
					dtm.addRow(new Object[] {
							null,
							"已选房间:",
							list.size()+"间",
							"房价总计:",
							sumRoomMoney		
					});
				}
					
			}
			
		}
	private static void addPopup(Component component, final JPopupMenu popup,JTable table) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				int index = table.rowAtPoint(e.getPoint());
				table.setRowSelectionInterval(index, index);
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	public TwoRoomTable2(AddCustomer ac) {
		this();
		this.ac = ac;
	}
	public void initList() {
		JComboBox roomType = ac.getCb_roomType();
		Object obj = roomType.getSelectedItem();
		Dictionary d = (Dictionary)obj;
		List<Room> rooms = roomService.findByRoomType(TypeID.房间类型);
		for (Room room : rooms) {
			if(room.getStatus().equals("空房")){
				roomList.add(room);
			}
		}
	}
}
